package org.commentservice.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.commentservice.entity.Comment;
import org.commentservice.service.CommentService;
import org.commentservice.utils.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.time.Instant;
import java.util.List;

@CrossOrigin
@Tag(name = "评论管理",description = "评论管理相关接口")
@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;

    @Operation(summary = "添加评论")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "添加成功"),
            @ApiResponse(responseCode = "400", description = "已评论过"),
            @ApiResponse(responseCode = "500", description = "添加失败")
    })
    @PostMapping({"/add"})//http://localhost:8089/main/comment/add
    public CommonResult addComment(@RequestBody Comment comment) {
        CommonResult commonResult = new CommonResult();
        try {
            Comment exComment = commentService.findByHouseIdAndTenantId(comment.getHouseId(), comment.getTenantId());
            if (exComment != null) {
                commonResult.setCode(400);
                commonResult.setMsg("已评论过");
            } else {
                commentService.add(comment);
                commonResult.setCode(200);
                commonResult.setMsg("添加成功");
            }
        }catch (Exception e) {
            commonResult.setCode(500);
            commonResult.setMsg("添加失败");
        }
        return commonResult;
    }

    @Operation(summary = "删除评论")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "删除成功"),
            @ApiResponse(responseCode = "500", description = "删除失败")
    })
    @DeleteMapping("/delete/{commentId}")//http://localhost:8089/main/comment/delete/{commentId}
    public CommonResult delete(@PathVariable long commentId) {
        CommonResult commonResult = new CommonResult();
        try{
            commentService.deleteById(commentId);
            commonResult.setCode(200);
            commonResult.setMsg("删除成功");
        }catch (Exception e) {
            commonResult.setCode(500);
            commonResult.setMsg("删除成功");
        }
        return commonResult;
    }

    @Operation(summary = "修改评论")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "修改成功"),
            @ApiResponse(responseCode = "500", description = "修改失败")
    })
    @PutMapping("/update")//http://localhost:8089/main/comment/update
    public CommonResult update(@RequestBody Comment comment) {
        CommonResult commonResult = new CommonResult();
        try{
            Timestamp commentTime = Timestamp.from(Instant.now());
            comment.setCommentTime(commentTime);
            commentService.update(comment);
            commonResult.setCode(200);
            commonResult.setMsg("修改成功");
        }catch (Exception e) {
            commonResult.setCode(500);
            commonResult.setMsg("修改成功");
        }
        return commonResult;
    }


    @Operation(summary = "查询指定房源的所有评论")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "404", description = "未查询到"),
            @ApiResponse(responseCode = "500", description = "查询失败")
    })
    @GetMapping("/house/{houseId}")//http://localhost:8089/main/comment/house/{houseId}
    public CommonResult getByHouse(@PathVariable long houseId) {
        CommonResult commonResult = new CommonResult();
        try{
            List<Comment> comments = commentService.findByHouseId(houseId);
            if (!comments.isEmpty()) {
                commonResult.setCode(200);
                commonResult.setMsg("查询成功");
                commonResult.setData(comments);
            }
            else{
                commonResult.setCode(404);
                commonResult.setMsg("未查询到");
            }
        }catch (Exception e){
            commonResult.setCode(500);
            commonResult.setMsg("查询失败");
        }
        return commonResult;
    }

    @Operation(summary = "查询某租户发表的所有评论")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "404", description = "未查询到"),
            @ApiResponse(responseCode = "500", description = "查询失败")
    })
    @GetMapping("/tenant/{tenantId}")//http://localhost:8089/main/comment/tenant/{tenantId}
    public CommonResult getByTenant(@PathVariable long tenantId) {
        CommonResult commonResult = new CommonResult();
        try{
            List<Comment> comments = commentService.findByTenantId(tenantId);
            if (!comments.isEmpty()) {
                commonResult.setCode(200);
                commonResult.setMsg("查询成功");
                commonResult.setData(comments);
            }
            else{
                commonResult.setCode(404);
                commonResult.setMsg("未查询到");
            }
        }catch (Exception e){
            commonResult.setCode(500);
            commonResult.setMsg("查询失败");
        }
        return commonResult;
    }

    @Operation(summary = "查询所有")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "404", description = "未查询到"),
            @ApiResponse(responseCode = "500", description = "查询错误")
    })
    @GetMapping("/findAll")//http://localhost:8089/main/comment/findAll
    public CommonResult findAll() {
        CommonResult commonResult = new CommonResult();
        try{
            List<Comment> comments = commentService.findAll();
            if (!comments.isEmpty()) {
                commonResult.setCode(200);
                commonResult.setMsg("查询成功");
                commonResult.setData(comments);
            }else{
                commonResult.setCode(404);
                commonResult.setMsg("未查询到");
            }
        }catch (Exception e) {
            commonResult.setCode(500);
            commonResult.setMsg("查询失败");
        }
        return commonResult;
    }

}
